package com.blandware.atleap.webapp.action.testimonials;

import com.blandware.atleap.common.TestimonialModuleConstants;
import com.blandware.atleap.model.testimonials.Testimonial;
import com.blandware.atleap.service.testimonials.TestimonialManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.ControllerSupport;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>RandomTestimonialController class.  This class is used to display a
 * random testimonial item.
 * </p>
 * <p><a href="RandomTestimonialController.java.html"><i>View Source</i></a></p>
 *
 * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
 * @version $Revision: 1.4 $ $Date: 2005/08/05 17:48:26 $
 */
public final class RandomTestimonialController extends ControllerSupport {

	protected transient final Log log = LogFactory.getLog(RandomTestimonialController.class);

	//~ Methods ================================================================
	/**
	 * Retrieves all menu items and puts them into request
	 *
	 * @param tilesContext   Current tile context
	 * @param request        Current request
	 * @param response       Current response
	 * @param servletContext Current Servlet Context
	 */
	public void execute(ComponentContext tilesContext,
	                    HttpServletRequest request,
	                    HttpServletResponse response,
	                    ServletContext servletContext) throws Exception {

		ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		TestimonialManager testimonialManager = (TestimonialManager) applicationContext.getBean(TestimonialModuleConstants.TESTIMONIAL_MANAGER_BEAN);
		Testimonial testimonial = testimonialManager.getRandomTestimonial();
		request.setAttribute("randomTestimonial", testimonial);
	}
}
